package collector.parsers;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import collector.support.URLHandler;


public class CurrencyConverter {
	private String to;
	private String from;
	private URLHandler urlHandler;

	public CurrencyConverter(String from, String to) {
		this.from = from;
		this.to = to;
		urlHandler = URLHandler.create("http://www.google.com/finance/converter?a=1&from="+from+"&to="+to);
	}

	public Double toSEK() {
		if (from == to)
			return 1.0;
		
		String content = urlHandler.getContent();
		//<span class=bld>1.2066 SEK</span>
		Pattern pattern = Pattern.compile("<span class=bld>(.*) "+to+"</span>",
				Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(content);
		String matchedString = null;
		if (matcher.find()) {
			matchedString = matcher.group(1);
			matchedString = matchedString.trim();
		}
		
		return Double.parseDouble(matchedString);
	}
}
